package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetAppointment;
import com.example.yiwei.crm.entity.ApointmentEntity;
import com.example.yiwei.crm.json.ApointmentJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.Class.listviewClass.ManyiduAdapter;
import com.example.yiwei.crm.view.activity.coordinationActivity.DetailedManyiduActivity;
import com.example.yiwei.crm.view.activity.coordinationActivity.ManYiDuActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class JYCoorJDActivity  extends AppCompatActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    ImageView fabIconStar;
    FloatingActionMenu leftCenterMenu;
    //    FloatingActionMenu leftCenterMenu1;
    FloatingActionButton leftCenterButton;
    FloatingActionButton leftCenterButton1;
    ImageView lcIcon1 ;
    ImageView lcIcon2 ;
    ImageView lcIcon3 ;
    private AutoListView listView;
    private ManyiduAdapter adapter;
    AlertDialog.Builder builder;
    ImageView img_back;
    int customer_id;
    int a=1;
    int page = 1;
    private List<ApointmentEntity> list;
    private List<ApointmentEntity> alist;
    int appoint_id;
    String manyidu;
    String theme[]={"非常满意","满意","一般","不满意"};
    private ImageView img_luru,img_find,img_tj,img_me;
    private SharedPreferences sp;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<ApointmentEntity> result = (List<ApointmentEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    if (list!=null){
                        list.addAll(result);
                    }else {
                        list = result;
                    }
                    if (a==1){
                        adapter = new ManyiduAdapter(JYCoorJDActivity.this,list);
                        listView.setAdapter(adapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    if (result!=null){
                        listView.onLoadComplete();
                        list.addAll(result);
                    }else {
                        listView.onLoadComplete();
                    }
                    break;
            }
            if (result!=null) {
//                listView.setResultSize(result.size());
            }else{
//                listView.setResultSize(0);
            }
            adapter.notifyDataSetChanged();
        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jycoor_jd);
        listView= (AutoListView) findViewById(R.id.simple_health_education_listview);
        listView.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        listView.setDividerHeight(1);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        GetAppointment.create();
        GetAppointment.apoint=null;
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoorJDActivity.this, JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();

            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoorJDActivity.this, JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoorJDActivity.this, JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoorJDActivity.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("KAA02",getIntent().getStringExtra("KAA02"));
                    object.put("KLA06",getIntent().getStringExtra("KLA06"));
                    object.put("KAA09N",getIntent().getStringExtra("KAA09N"));
                    object.put("KAA23",getIntent().getStringExtra("KAA23"));
                    object.put("KAA24",getIntent().getStringExtra("KAA24"));
                    object.put("KAA25",getIntent().getStringExtra("KAA25"));
                    object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                    object.put("CTS01E",getIntent().getStringExtra("CTS01E"));
                    object.put("KYA11",getIntent().getStringExtra("KYA11"));

                    object.put("UAA02N",getIntent().getStringExtra("UAA02N"));
                    object.put("AKB01",getIntent().getIntExtra("AKB01",0));
                    object.put("AKM01",getIntent().getIntExtra("AKM01",0));
                    object.put("AKC01",getIntent().getIntExtra("AKC01",0));
                    object.put("AKD01",getIntent().getIntExtra("AKD01",0));
                    object.put("KYA02_B",getIntent().getStringExtra("KYA02_B"));
                    object.put("KYA02_E",getIntent().getStringExtra("KYA02_E"));
                    object.put("KYA13F",getIntent().getStringExtra("KYA13F"));
                    object.put("KYA09_B",getIntent().getStringExtra("KYA09_B"));
                    object.put("KYA09_E",getIntent().getStringExtra("KYA09_E"));
                    object.put("KYA76_B",getIntent().getStringExtra("KYA76_B"));
                    object.put("KYA76_E",getIntent().getStringExtra("KYA76_E"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                String result = NetWorkUtils.getCount(baseClass.find_AllYY_URL+1,json);
                list = ApointmentJson.getApointment(result);
                if (list!=null){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(list!=null){
                                adapter = new ManyiduAdapter(JYCoorJDActivity.this,list);
                                listView.setAdapter(adapter);
                                a++;
                                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        GetAppointment.create();
                                        GetAppointment.apoint = list.get(position-1);
                                        Intent intent = new Intent(JYCoorJDActivity.this,JYDetiledJDActivity.class);
                                        intent.putExtra("KYA01",list.get(position-1).getAppoint_id());
                                        intent.putExtra("KAA01",list.get(position-1).getCustomer_id());
                                        intent.putExtra("is_add",list.get(position-1).getIs_add());
                                        startActivity(intent);
                                    }
                                });
                            }
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(JYCoorJDActivity.this, "没有记录", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        }).start();
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }

    public void onClick(View view) {
        switch (view.getId()){

        }
    }

    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                object.put("KAA02",getIntent().getStringExtra("KAA02"));
                object.put("KLA06",getIntent().getStringExtra("KLA06"));
                object.put("KAA09N",getIntent().getStringExtra("KAA09N"));
                object.put("KAA23",getIntent().getStringExtra("KAA23"));
                object.put("KAA24",getIntent().getStringExtra("KAA24"));
                object.put("KAA25",getIntent().getStringExtra("KAA25"));
                object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                object.put("CTS01E",getIntent().getStringExtra("CTS01E"));
                object.put("KYA11",getIntent().getStringExtra("KYA11"));

                object.put("UAA02N",getIntent().getStringExtra("UAA02N"));
                object.put("AKB01",getIntent().getIntExtra("AKB01",0));
                object.put("AKM01",getIntent().getIntExtra("AKM01",0));
                object.put("AKC01",getIntent().getIntExtra("AKC01",0));
                object.put("AKD01",getIntent().getIntExtra("AKD01",0));
                object.put("KYA02_B",getIntent().getStringExtra("KYA02_B"));
                object.put("KYA02_E",getIntent().getStringExtra("KYA02_E"));
                object.put("KYA13F",getIntent().getStringExtra("KYA13F"));
                object.put("KYA09_B",getIntent().getStringExtra("KYA09_B"));
                object.put("KYA09_E",getIntent().getStringExtra("KYA09_E"));
                object.put("KYA76_B",getIntent().getStringExtra("KYA76_B"));
                object.put("KYA76_E",getIntent().getStringExtra("KYA76_E"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_AllYY_URL+page,json);
            alist = ApointmentJson.getApointment(result);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                msg.obj = alist;
                handler.sendMessage(msg);
                Toast.makeText(JYCoorJDActivity.this, "无更多信息", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            page =1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
                object.put("KAA02",getIntent().getStringExtra("KAA02"));
                object.put("KLA06",getIntent().getStringExtra("KLA06"));
                object.put("KAA09N",getIntent().getStringExtra("KAA09N"));
                object.put("KAA23",getIntent().getStringExtra("KAA23"));
                object.put("KAA24",getIntent().getStringExtra("KAA24"));
                object.put("KAA25",getIntent().getStringExtra("KAA25"));
                object.put("CTS01B",getIntent().getStringExtra("CTS01B"));
                object.put("CTS01E",getIntent().getStringExtra("CTS01E"));
                object.put("KYA11",getIntent().getStringExtra("KYA11"));

                object.put("UAA02N",getIntent().getStringExtra("UAA02N"));
                object.put("AKB01",getIntent().getIntExtra("AKB01",0));
                object.put("AKM01",getIntent().getIntExtra("AKM01",0));
                object.put("AKC01",getIntent().getIntExtra("AKC01",0));
                object.put("AKD01",getIntent().getIntExtra("AKD01",0));
                object.put("KYA02_B",getIntent().getStringExtra("KYA02_B"));
                object.put("KYA02_E",getIntent().getStringExtra("KYA02_E"));
                object.put("KYA13F",getIntent().getStringExtra("KYA13F"));
                object.put("KYA09_B",getIntent().getStringExtra("KYA09_B"));
                object.put("KYA09_E",getIntent().getStringExtra("KYA09_E"));
                object.put("KYA76_B",getIntent().getStringExtra("KYA76_B"));
                object.put("KYA76_E",getIntent().getStringExtra("KYA76_E"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_AllYY_URL+1,json);
            alist = ApointmentJson.getApointment(result);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if (alist!=null){
                if(listView.getVisibility()==View.GONE){
                    listView.setVisibility(View.VISIBLE);
                }
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                listView.setVisibility(View.GONE);
            }
            super.onPostExecute(integer);
        }
    }
    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
            UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
           LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetAppointment.apoint1!=null){
            list.clear();
            list.addAll(GetAppointment.apoint1);
            adapter.notifyDataSetChanged();
            GetAppointment.apoint1=null;
        }else {
            onRefresh();
        }

    }
}
